package com.jason.apps.dashboard;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.app.SherlockListFragment;
import com.apps.essentials.ConsumeWebService;
import com.apps.realtyportal.R;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.forimageandlist.Adapter;
import com.jason.apps.alert.ErrorAlert;


public class DeleteAccount extends SherlockFragment{
	
	ConsumeWebService c;
	
	final static ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
	
	HashMap<String, String> map;
	private ListView list;
	
	Adapter adapter;
	SherlockFragmentActivity fa;
	
	public static final String KEY_FIRSTNAME = "firstName";
	public static final String KEY_LASTNAME = "lastName";
	public static final String KEY_MOBILE = "mobile";
	public static final String KEY_LANDLINE = "landLine";
	public static final String KEY_PERSON_ID = "person_id";
	public static final String KEY_EMAILADDRESS = "emailAddress";
	public static final String KEY_USERNAME = "userName";
	public static final String KEY_PASSWORD = "password";
	public static final String KEY_IMAGE = "person_image";
	public static final String KEY_BIRTHDAY = "birthday";
	
	public void displayList(){
		c = new ConsumeWebService();
		 String data = c.getAllPerson();
		 
		 try {

				JSONArray array = new JSONArray(data);
				
				for (int i = 0; i < array.length(); i++) {
					map = new HashMap<String, String>();
					JSONObject e = array.getJSONObject(i);
					map.put("person_id", e.getString("person_id"));
					map.put("firstName", e.getString(KEY_FIRSTNAME));
					map.put("lastName", e.getString(KEY_LASTNAME));
					map.put("mobile", e.getString(KEY_MOBILE));
					map.put("landLine", e.getString(KEY_LANDLINE));
					//map.put("person_id", e.getString(KEY_PERSON_ID));
					map.put("emailAddress", e.getString(KEY_EMAILADDRESS));
					map.put("username", e.getString(KEY_USERNAME));
					map.put("password", e.getString(KEY_PASSWORD));
					map.put("person_image", e.getString(KEY_IMAGE));
					mylist.add(map);

				}
			} catch (JSONException e) {
				Log.e("log_tag", "Error parsing data " + e.toString()+"diri jud");
			}finally{
				
			}
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		fa = (SherlockFragmentActivity) super.getActivity();
		View view=inflater.inflate(R.layout.listfordelete, container, false);
		
		mylist.clear();
		//displayList();
		adapter=new Adapter(getActivity(), mylist); 
		new DelAcc().execute();
		
		list=(ListView)view.findViewById(R.id.list);
		Button delete=(Button)view.findViewById(R.id.btnDelete);
		delete.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				// TODO Auto-generated method stub
				new Delete().execute();
			}
			
		});
       
		return view;
    }
	
	//Asynctask
		public class Delete extends AsyncTask<Void, Void, Void> {

		    private final ProgressDialog dialog = new ProgressDialog(fa);

		    protected void onPreExecute() {

		            this.dialog.setMessage("Loading...");
		            this.dialog.show();
		    }


		    protected Void doInBackground(final Void... unused) {
		    	//System.out.println("Value:"+Adapter.idList.toString());
		    	try{
		    		if(!Adapter.idList.isEmpty()){
		    			
		    			for(int i=0;i<Adapter.idList.size();i++){
		    				c.deletePerson(Adapter.idList.get(i));
		    				System.out.println("Deleted");
		    			}	
		    			
		    		}
		    		else{
		    			FragmentManager fm = getActivity().getSupportFragmentManager();
					     ErrorAlert alert = new ErrorAlert();
					     alert.show(fm, "Alert_Dialog");
		    		}
		    	
		    	}catch(Exception e){
		    		System.out.print(e.getMessage());
		    	}
		        return null; 
		    }
		    protected void onPostExecute(Void result) {
		    	mylist.clear();
		    	displayList();
		    	adapter=new Adapter(getActivity(), mylist);  
		    	list.setAdapter(adapter);
		    	if (this.dialog!=null) {
		            this.dialog.dismiss();
		            }  
		     }
		  }
		
		//Asynctask
		public class DelAcc extends AsyncTask<Void, Void, Void> {

				    private final ProgressDialog dialog = new ProgressDialog(fa);

				    protected void onPreExecute() {

				            this.dialog.setMessage("Loading...");
				            this.dialog.show();
				    }


				    protected Void doInBackground(final Void... unused) {
				    	//getAllLabels();
				    	displayList();
				        return null; 
				    }
				    protected void onPostExecute(Void result) {
				    	//loadSpinner(agentspinner);
				        list.setAdapter(adapter);
				    	if (this.dialog!=null) {
				            this.dialog.dismiss();
				            }  
				     }
				  }
}
